﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BL;
using DA;
using Entities;

namespace ERP
{
    public partial class RoleEdit : System.Web.UI.Page
    {
        RoleManager roleManager = new RoleManager();

        string roleIdString;
        int? roleId;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                roleIdString = Request.QueryString.Get("RoleId");
                roleId = roleIdString.ToInt();
                if (roleId.HasValue)
                {
                    ERP_Roles role = roleManager.Getrole(roleId.Value);
                    if (role != null)
                    {
                        txtName.Text = role.Name;
                        txtPermmisions.Text = role.Permmisions;
                        txtRoledescription.Text = role.RoleDescription;
                    }
                    else
                    {

                        ShowError("The user was not found.");
                    }
                }
            }
        }
        public void btnSubmit_OnClick(object sender, EventArgs e)
        {
            roleIdString = Request.QueryString.Get("RoleId");
            roleId = roleIdString.ToInt();

            if (roleId.HasValue)
            {
                if (roleManager.RoleExist(roleId.Value))
                {
                    ERP_Roles role = new ERP_Roles()
                        {
                            RoleId = roleId.Value,
                            Name = txtName.Text,
                            Permmisions = txtPermmisions.Text,
                            RoleDescription = txtRoledescription.Text
                        };

                    if (roleManager.EditRole(role))
                    {
                        ShowError("The user was updated.");
                        lblMessage.CssClass = "valid";
                    }
                    else
                    {
                        ShowError("Error. The user was not updated.");
                        lblMessage.CssClass = "failureNotification";
                    }
                }
            }
            else
            {
                ERP_Roles role = new ERP_Roles()
                    {
                        Name = txtName.Text,
                        Permmisions = txtPermmisions.Text,
                        RoleDescription = txtRoledescription.Text
                    };

                if (roleManager.AddRole(role))
                {
                    ShowError("The role was created.");
                    lblMessage.CssClass = "valid";
                }
                else
                {
                    ShowError("Error. The role was not created.");
                    lblMessage.CssClass = "failureNotification";
                }
            }
        }

        private void ShowError(string message)
        {
            this.lblMessage.Text = message;
            this.lblMessage.Visible = true;
        }
    }
}





